package com.vnp.cooking.utils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.LinearLayout;

public class LinearLayoutThatDetectsSoftKeyboard extends LinearLayout {
	public LinearLayoutThatDetectsSoftKeyboard(Context context, AttributeSet attrs) {
		super(context, attrs);
	}
	public LinearLayoutThatDetectsSoftKeyboard(Context context) {
		super(context);
	}

	public interface Listener {
		public void onSoftKeyboardShown(boolean isShowing);
	}

	private Listener listener;
	Boolean isLastState = false;
	
	public void setListener(Listener listener) {
		this.listener = listener;
	}

	@SuppressWarnings("deprecation")
	@SuppressLint("DrawAllocation")
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		try {
			int height = MeasureSpec.getSize(heightMeasureSpec);
			Activity activity = (Activity) getContext();
			Rect rect = new Rect();
			activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
			int statusBarHeight = rect.top;
			int screenHeight = activity.getWindowManager().getDefaultDisplay().getHeight();
			int diff = (screenHeight - statusBarHeight) - height;
			if (listener != null) {
				if(isLastState != (diff > 128)){
					isLastState = diff > 128;
					listener.onSoftKeyboardShown(isLastState); // assume all soft
				}
			}
			super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		} catch (Exception e) { }
		
		
	}

}
